/*
H.264 encoder/decoder plugin for mediastreamer2 based on the openh264 library.
Copyright (C) 2006-2012 Belledonne Communications, Grenoble

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/msvideo.h"
#include "mediastreamer2/rfc3984.h"

#include "ohos_video_common.h"
#include "ohos_video_data.h"

#include <multimedia/player_framework/native_avbuffer.h>
#include <multimedia/player_framework/native_avcapability.h>
#include <multimedia/player_framework/native_avcodec_base.h>
#include <multimedia/player_framework/native_avcodec_videodecoder.h>
#include <multimedia/player_framework/native_avformat.h>

class OHOSVideoH264Decoder {
public:
	OHOSVideoH264Decoder(MSFilter *f);
	virtual ~OHOSVideoH264Decoder();
	bool isInitialized() const {
		return mInitialized;
	}
	void initialize();
	void feed();
	void uninitialize();
	void provideSpropParameterSets(char *value, size_t valueSize);
	void resetFirstImageDecoded();
	void enableAVPF(bool enable) {
		mAVPFEnabled = enable;
	}
	MSVideoSize getSize() const;
	float getFps() const;
	const MSFmtDescriptor *getOutFmt() const;

	class DataCallback {
	public:
		static void OnCodecError(OH_AVCodec *codec, int32_t errorCode, void *userData);
		static void OnCodecFormatChange(OH_AVCodec *codec, OH_AVFormat *format, void *userData);
		static void OnNeedInputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData);
		static void OnNewOutputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData);
	};

	ohos::CodecData *GetCodecData();
	int32_t FreeInputData(int32_t index);
	int32_t FreeOutputData(uint32_t index);
	int32_t GetFormatInfo(ohos::FormatType type);
	void feedNV21AsI420(uint8_t *addr, int width, int height, int stride);

private:
	int nalusToFrame(MSQueue *nalus);
	void enlargeBitstream(int newSize);

	MSFilter *mFilter;
	Rfc3984Context *mUnpacker;
	MSPicture mOutbuf;
	MSAverageFPS mFPS;
	mblk_t *mSPS;
	mblk_t *mPPS;
	mblk_t *mYUVMsg;
	uint8_t *mBitstream;
	int mBitstreamSize;
	uint64_t mLastErrorReportTime;
	int mWidth;
	int mHeight;
	bool mInitialized;
	bool mFirstImageDecoded;
	bool mAVPFEnabled;

	int32_t stride_{0};
	mutable std::mutex inputMutex_;
	std::condition_variable inputCond_;
	std::atomic<int32_t> errorCode_{0};
	struct OH_AVFormat *avFormat_{nullptr};
	std::atomic_bool fmtChanged_{false};
	OH_AVCodec *decoder_{nullptr};
	std::shared_ptr<ohos::VideoFormat> format_{nullptr};
	std::shared_ptr<ohos::CodecData> data_{nullptr};
};
